<template>
  <div>
    <el-form label-position="left" label-width="250px" style="width:100%;">
      <el-form-item label="单日优惠最大次数限制："><el-input-number v-model="formData.discountNum" placeholder="请输入单日优惠最大次数限制" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="有订单时超时时间限制(分钟)："><el-input-number v-model="formData.orderTimeLimit" placeholder="请输入有订单时超时时间限制(分钟)" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="有订单时超时时间加时限制(分钟)："><el-input-number v-model="formData.orderTimeAddLimit" placeholder="请输入有订单时超时时间加时限制(分钟)" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="订单在离开前生成时间限制(分钟)："><el-input-number v-model="formData.beforeOrderTimeLimit" placeholder="请输入订单在离开前生成时间限制(分钟)" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="订单在离开后生成时间限制(分钟)："><el-input-number v-model="formData.afterOrderTimeLimit" placeholder="请输入订单在离开后生成时间限制(分钟)" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="短途最大路程长度限制(公里)："><el-input-number v-model="formData.orderDistanceLimit" placeholder="请输入短途最大路程长度限制(公里)" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="优先次数重置时间点："><el-input v-model="formData.refreshTime" placeholder="请输入优惠次数重置时间点" class="filter-item" /></el-form-item>
      <el-form-item label="是否离线：">
        <el-radio-group v-model="formData.offLine">
          <el-radio-button label="0">否</el-radio-button>
          <el-radio-button label="1">是</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="重复扫描间隔秒数："><el-input-number v-model="formData.reScanLimit" placeholder="请输入重复扫描间隔秒数" controls-position="right" class="filter-item" /></el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click="cancel">取消</el-button>
      <el-button v-waves :loading="confirmLoading" type="primary" icon="el-icon-check" @click="submit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getSysSettingDetail, editSysSetting } from '@/api/setting';
import waves from '@/directive/waves';

export default {
  name: 'SysSettingAdd',
  directives: { waves },
  props: {
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      confirmLoading: false,
      formData: {
      }
    };
  },
  created() {
    if (this.id) {
      this.getDetail(this.id);
    }
  },
  methods: {
    async getDetail(id) {
      await getSysSettingDetail(id).then(response => {
        this.formData = response.data;
      });
    },
    async submit() {
      if (this.formData.discountNum == null) {
        this.$message.error('请输入单日优惠最大次数限制');
        return;
      }
      if (this.formData.orderTimeLimit == null) {
        this.$message.error('请输入有订单时超时时间限制(分钟)');
        return;
      }
      if (this.formData.orderTimeAddLimit == null) {
        this.$message.error('请输入有订单时超时时间加时限制(分钟)');
        return;
      }
      if (this.formData.beforeOrderTimeLimit == null) {
        this.$message.error('请输入订单在离开前生成时间限制(分钟)');
        return;
      }
      if (this.formData.afterOrderTimeLimit == null) {
        this.$message.error('请输入订单在离开后生成时间限制(分钟)');
        return;
      }
      if (this.formData.orderDistanceLimit == null) {
        this.$message.error('请输入短途最大路程长度限制(公里)');
        return;
      }
      if (this.formData.refreshTime === '') {
        this.$message.error('请输入优惠次数重置时间点');
        return;
      }
      if (this.formData.reScanLimit === '') {
        this.$message.error('请输入重复扫描间隔秒数');
        return;
      }
      this.confirmLoading = true;
      if (this.id) {
        await editSysSetting(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('编辑成功');
          this.$emit('cancelClose', true, 'edit', null);
        });
      }
    },
    cancel() {
      if (this.id) {
        this.$emit('cancelClose', false, 'edit', null);
      }
    }
  }
};
</script>
